home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / modules / clientData.js < prev    next >
Text File  |  2008-07-31  |  4KB  |  130 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Chris Beard <chris@mozilla.com>
  22.  *  Dan Mills <thunder@mozilla.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const EXPORTED_SYMBOLS = ['ClientData'];
  39.  
  40. const Cc = Components.classes;
  41. const Ci = Components.interfaces;
  42. const Cr = Components.results;
  43. const Cu = Components.utils;
  44.  
  45. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  46. Cu.import("resource://weave/log4moz.js");
  47. Cu.import("resource://weave/util.js");
  48. Cu.import("resource://weave/dav.js");
  49. Cu.import("resource://weave/remote.js");
  50. Cu.import("resource://weave/async.js");
  51.  
  52. Function.prototype.async = Async.sugar;
  53.  
  54. Utils.lazy(this, 'ClientData', ClientDataSvc);
  55.  
  56. function ClientDataSvc() {
  57.   this._init();
  58. }
  59. ClientDataSvc.prototype = {
  60.   get GUID() {
  61.     return this._getCharPref("client.GUID", function() Utils.makeGUID());
  62.   },
  63.   set GUID(value) {
  64.     Utils.prefs.setCharPref("client.GUID", value);
  65.   },
  66.  
  67.   get name() {
  68.     return this._getCharPref("client.name", function() "Firefox");
  69.   },
  70.   set GUID(value) {
  71.     Utils.prefs.setCharPref("client.name", value);
  72.   },
  73.  
  74.   get type() {
  75.     return this._getCharPref("client.type", function() "desktop");
  76.   },
  77.   set GUID(value) {
  78.     Utils.prefs.setCharPref("client.type", value);
  79.   },
  80.  
  81.   clients: function ClientData__clients() {
  82.     return this._remote.data;
  83.   },
  84.  
  85.   _getCharPref: function ClientData__getCharPref(pref, defaultCb) {
  86.     let value;
  87.     try {
  88.       value = Utils.prefs.getCharPref(pref);
  89.     } catch (e) {
  90.       value = defaultCb();
  91.       Utils.prefs.setCharPref(pref, value);
  92.     }
  93.     return value;
  94.   },
  95.  
  96.   _init: function ClientData__init() {
  97.     this._log = Log4Moz.Service.getLogger("Service.ClientData");
  98.     this._remote = new Resource("meta/clients");
  99.     this._remote.pushFilter(new JsonFilter());
  100.   },
  101.  
  102.   _wrap: function ClientData__wrap() {
  103.     return {
  104.       GUID: this.GUID,
  105.       name: this.name,
  106.       type: this.type
  107.     };
  108.   },
  109.  
  110.   _refresh: function ClientData__refresh() {
  111.     let self = yield;
  112.  
  113.     let ret = yield DAV.MKCOL("meta", self.cb);
  114.     if(!ret)
  115.       throw "Could not create meta information directory";
  116.  
  117.     try { yield this._remote.get(self.cb); }
  118.     catch (e if e.status == 404) {
  119.       this._remote.data = {};
  120.     }
  121.  
  122.     this._remote.data[this.GUID] = this._wrap();
  123.     yield this._remote.put(self.cb);
  124.     this._log.debug("Successfully downloaded clients file from server");
  125.   },
  126.   refresh: function ClientData_refresh(onComplete) {
  127.     this._refresh.async(this, onComplete);
  128.   }
  129. };
  130.